package com.cskaoyan.service;

import com.cskaoyan.bean.bo.BrandBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.vo.BrandVO;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.mapper.BrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author by WANGJun
 * @date 2022/6/5 15:15
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Override
    public TVO<BrandVO> listBrand(BaseParam baseParam,Integer id,String name) {
        List<BrandVO> brandVOS = brandMapper.selectBrandByPage(baseParam, id, name);
        int total = brandMapper.selectBrandCount();
        TVO<BrandVO> brandVOTVO = new TVO<>();
        brandVOTVO.setTotal(total);
        brandVOTVO.setPages((int) Math.ceil((total+0.0)/baseParam.getLimit()));
        brandVOTVO.setLimit(baseParam.getLimit());
        brandVOTVO.setPage(baseParam.getPage());
        brandVOTVO.setList(brandVOS);
        return brandVOTVO;
    }

    @Override
    public BrandVO createBrand(BrandBO brandBO) {
        brandMapper.addBrand(brandBO);
        BrandVO brandVO = brandMapper.selectBrandById(brandBO.getId());
        return brandVO;
    }

    @Override
    public BrandVO updateBrand(BrandBO brandBO) {
        brandMapper.updateBrand(brandBO);
        BrandVO brandVO = brandMapper.selectBrandById(brandBO.getId());
        return brandVO;
    }

    @Override
    public int deleteBrand(Integer id) {
        int deleteBrand = brandMapper.deleteBrand(id);
        return deleteBrand;
    }
}
